﻿using System;
using System.Collections.Generic;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ESSManageService.IServices;
using ESSManageService.IServices.IDBServices;
using ESSManageService.Model.DBModels.Info;
using Microsoft.VisualBasic;

namespace ESSManageService.Services.DBServices
{
    public class UserInfoServices : SqlSugarRepository<UserInfo>, IUserInfoServices
    {
        public bool DeleteByID(long Id)
        {
            //Db.Ado.ExecuteCommand("SET IsDeleted=0");
            //Db.Ado.SqlQuery<bool>("SET IsDeleted=0");
            return base.Delete(it => it.Id == Id);
        }

        /// <summary>
        /// 验证用户登录
        /// </summary>
        /// <param name="loginName"></param>
        /// <param name="loginPwd"></param>
        /// <returns></returns>
        public bool GetUserRoleNameStr(string loginName, string loginPwd)
        {
            //查找符合loginName和loginPwd的用户
            var user = Db.Queryable<UserInfo>()
                .Where(it => it.LoginName == loginName && it.LoginPWD == loginPwd)
                .First();
            if (user == null)
                return false;
            return true;
        }

        public Task<List<UserInfo>> GetAllUserInfo(string orderBy, out int pageCount,out int totalCount, int page = 1, int PageSize = 50)
        {
            pageCount = 0;
            totalCount = 0;
            try
            {
                var list = Db.Queryable<UserInfo>()
                .OrderByIF(!string.IsNullOrEmpty(orderBy), orderBy)
                .ToPageList(page, PageSize, ref totalCount, ref pageCount);

                return Task.FromResult(list);
            }
            catch (Exception ex)
            {
                return Task.FromResult(new List<UserInfo>());
            }
        }

        public Task<UserInfo> SaveUserInfo(string loginName, string loginPwd)
        {
            throw new NotImplementedException();
        }
        public UserInfo GetUserInfo(string loginName, string loginPwd)
        {
            return Db.Queryable<UserInfo>()
                .Where(it => it.LoginName == loginName && it.LoginPWD == loginPwd)
                .First();
        }

        public UserInfo GetUserInfo(long Id)
        {
            return Db.Queryable<UserInfo>()
                .Where(it => it.Id == Id)
                .First();
        }
    }
}
